package com.itheima.blog.auth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.blog.auth.dto.LoginReqDto;
import com.itheima.blog.auth.dto.LoginResDTO;
import com.itheima.blog.auth.dto.UserDto;
import com.itheima.blog.auth.entity.WebUser;
import com.itheima.blog.auth.dao.WebUserMapper;
import com.itheima.blog.auth.service.WebUserService;
import com.itheima.blog.util.BeanHelper;
import com.itheima.blog.util.JwtUtils;
import com.itheima.blog.util.constants.Constants;
import com.itheima.blog.util.constants.ErrorInfo;
import com.itheima.common.domain.R;
import com.itheima.common.exception.BadRequestException;
import com.itheima.common.exception.BlogException;
import com.itheima.common.utills.CollUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 门户用户信息表 服务实现类
 * </p>
 *
 * @author HM
 * @since 2023-05-25
 */
@Service
public class WebUserServiceImpl extends ServiceImpl<WebUserMapper, WebUser> implements WebUserService {

    @Autowired
    private BCryptPasswordEncoder encoder;
    @Autowired WebUserMapper webUserMapper;

    @Override
    public LoginResDTO login(LoginReqDto dto) {
        // 根据用户名查用户信息
        LambdaQueryWrapper<WebUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WebUser::getName, dto.getName());
        WebUser userDB = this.getOne(queryWrapper);
        if(userDB == null){
            throw new BlogException(ErrorInfo.Msg.USER_NOT_EXISTS);
        }
        // 密码的匹配
        String passwordDB = userDB.getPassword();
        String passwordInput = dto.getPassword();
        boolean matches = encoder.matches(passwordInput, passwordDB);
        if(!matches){
            throw new BlogException(ErrorInfo.Msg.PASSWORD_ERROR);
        }
        // 生成token，user返回
        LoginResDTO result = new LoginResDTO();
        result.setUser(BeanHelper.copyProperties(userDB, UserDto.class));
        result.setToken(JwtUtils.generateTokenExpireInMinutes(userDB.getId(), Constants.EXPIRE_MINUTE));
        return result;
    }

    /**
     * 根据用户id查询
     * @param userIds
     * @return
     */
    @Override
    public List<WebUser> findUserList(List<Integer> userIds) {
        if (CollUtils.isEmpty(userIds)){
            throw new BadRequestException("用户id不能为空");
        }
//        return webUserMapper.selectBatchIds(userIds);
        return lambdaQuery().in(WebUser::getId, userIds).list();
    }

    /**
     * 根据用户昵称查询userIdS
     * @param nickName
     * @return
     */
    @Override
    public List<Integer> findUserIdListByNickName(String nickName) {
        List<WebUser> webUsers = lambdaQuery().like(WebUser::getNickname, nickName).list();
        List<Integer> userIds = webUsers.stream().map(WebUser::getId).collect(Collectors.toList());
        return userIds;
    }
}
